package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.PubStoryAdapter;
import reduce.fz.com.reduce.bean.ExtraParam;
import reduce.fz.com.reduce.bean.Pic;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.StoryCard;
import reduce.fz.com.reduce.bean.StoryPicCard;
import reduce.fz.com.reduce.bean.TeamSubject;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.event.PubStoryEvent;
import reduce.fz.com.reduce.init.RecycleOper;
import reduce.fz.com.reduce.photo.PhotoWallActivity;
import reduce.fz.com.reduce.service.StoryService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.oss.Oss;
import reduce.fz.com.reduce.util.photo.PhotoSelect;
import reduce.fz.com.reduce.util.photo.SelectParam;

public class PubShareStoryActivity extends BaseToolBarActivity implements View.OnClickListener {

    //发布经验故事列表
    public RecyclerView mStoryListView;
    //故事卡片
    private List<StoryPicCard> picCards = null;
    //故事服务
    private StoryService storyService = ServiceBuilder.builderStoryService(null);
    //发布故事
    private PubStoryAdapter mAdapter;
    //上传图片操作
    private Oss oss = Oss.instance();
    //保存按钮
    private TextView tvSave;
    //编辑内容操作类型
    public static final int EDITCONTENT = 121;
    //选择图片
    private List<String> seletPaths = null;
    //图片选择操作
    private PhotoSelect photoSelect;
    //记录当前编辑位置
    private int editPosition;
    //当前编辑卡片
    private StoryPicCard editcard;
    //卡片
    private StoryCard storyCard;

    private ExtraParam params;

    private Long teamid;
    //加载狂
    private LoadingDialog dialog = null;
    public void initPhotoSelect() {
        SelectParam param = new SelectParam();
        param.selectMax = 1;
        photoSelect = PhotoSelect.instance(this, param);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pub_share_story);
        params= (ExtraParam) getIntent().getSerializableExtra("extraobj");
        teamid=Long.parseLong(params.get("teamid"));
        setToolbarTitle("发布经历");
        tvSave=setRightBtn("发布");
        initWidget();
        initRecycleViewConfig();
        initdata();
        initPhotoSelect();
        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pubStory();
            }
        });
    }

    public void initdata() {
        seletPaths = getIntent().getStringArrayListExtra(PhotoWallActivity.IMAGE_SELECT_RESULT);
        mAdapter.init(seletPaths);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PhotoSelect.SELECT_PIC:
                ArrayList<String> paths = photoSelect.getSelectPaths(data);
                editcard.localpth = paths.get(0);
                mAdapter.notifyContentItemChanged(editPosition);
                break;
            case EDITCONTENT:
                if(data==null) return;
                String content = data.getStringExtra("pictext");
                editcard.desc = content;
                mAdapter.notifyContentItemChanged(editPosition);
                break;
            default:
                break;
        }
    }


    public void pubStory() {
        dialog = new LoadingDialog(this);
        dialog.setTitle("提交中,请稍候");
        dialog.show();
        storyCard = mAdapter.getStoryCard();
        storyCard.teamid=teamid;
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < storyCard.picCards.size(); i++)
        {
            list.add(storyCard.picCards.get(i).localpth);
        }
        oss.uploadPics(list, new Oss.OssUploadResponse() {
            @Override
            public void onSuccess(List<Pic> successPics, List<Pic> failedPics, List<Pic> allPics) {
                for (int i = 0; i < storyCard.picCards.size(); i++) {
                    storyCard.picCards.get(i).cardurl=allPics.get(i);
                }
                submitInfo();
            }

            @Override
            public void onPrgress(int complete, int total) {
                dialog.setTitle("上传图片中" + complete+1 + "/" + total);

            }
        });
    }

    public void submitInfo() {
        dialog.setTitle("提交中");
        storyService.pubStoryCard(storyCard, new
                        AbResponseListener<StoryCard>(new TypeReference<Result<StoryCard>>() {
                        }) {
                            @Override
                            public void error(ServiceException e) {
                                dialog.dismiss();

                            }

                            @Override
                            public void responseInfo(Result<StoryCard> data) {
                                dialog.dismiss();
                                User user= UserPreference.getUser();
                                StoryCard card=data.getData();
                                card.user=user;
                                EventBus.getDefault().post(new PubStoryEvent(card));
                                finish();
                            }
                        }
        );
    }

    public void initRecycleViewConfig() {
        mAdapter = new PubStoryAdapter(this, new PubStoryAdapter.OnPicItemListener() {
            @Override
            public void onSelectImage(int position, StoryPicCard card) {

                selectPic(position, card);

            }

            @Override
            public void onEditComment(int position, StoryPicCard card) {
                forwardEdit(position, card);
            }
        });

        mStoryListView.setAdapter(mAdapter);
        RecycleOper oper = new RecycleOper(this, mStoryListView);
        oper.setDefault();
    }

    public void selectPic(int position, StoryPicCard card) {
        editcard = card;
        editPosition = position;
        photoSelect.selectPhoto();
    }

    public void forwardEdit(int position, StoryPicCard card) {
        editcard = card;
        editPosition = position;
        Intent intent = new Intent(this, StoryPicTextEditActivity.class);
        startActivityForResult(intent, EDITCONTENT);
    }


    @Override
    public void initWidget() {
        mStoryListView = (RecyclerView) findViewById(R.id.story_list_view);
    }

    @Override
    public void initListener() {
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            default:
                break;
        }

    }


}
